﻿using Knight.Interface;
using Knight.Models.Base;
using Knight.Models.Entity;
using Knight.Models.Services;
using Knight.Web.Infrastructure;
using Knlght.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApp.Controllers
{
    public class HomeController : BaseWebController
    {
        // GET: Home
        public ActionResult Index(int page = 1, string goodsName = "", string classACommodity = "", string shopName = "", string platformType = "", string commodityPrice = "", string couponAmount = "")
        {
            int size = 20;
            var query = new QueryRequest<CouponInfo>();
            query.AddOrder(nameof(CouponInfo.CouponAmount), true);
            //商品名称
            if (!string.IsNullOrEmpty(goodsName))
            {
                query.Add(o => o.GoodsName.Contains(goodsName));
            }
            //商品一级类目
            if (!string.IsNullOrEmpty(classACommodity))
            {
                query.Add(o => o.ClassACommodity.Contains(classACommodity));
            }
            //店铺名称
            if (!string.IsNullOrEmpty(shopName))
            {
                query.Add(o => o.ShopName.Contains(shopName));
            }
            //平台类型
            if (!string.IsNullOrEmpty(platformType))
            {
                query.Add(o => o.PlatformType.Contains(platformType));
            }
            //价格排序
            if (!string.IsNullOrEmpty(commodityPrice))
            {
                query.AddOrder(nameof(CouponInfo.CommodityPrice), Convert.ToBoolean(commodityPrice));
            }
            //优惠券金额 
            if (!string.IsNullOrEmpty(couponAmount))
            {
                query.AddOrder(nameof(CouponInfo.CouponAmount), Convert.ToBoolean(couponAmount));
            }
            var service = Knight.ObjectFactory.ObjectFactory.Get<IDefaultService>();
            var pagingRequest = new PagingRequest<CouponInfo>
            {
                PageIndex = page,
                PageSize = size,
                Query = query
            };
            //优惠券
            var rsp = service.Paging(pagingRequest);
            ViewBag.Result = rsp;
            //类目
            var cQuery = new QueryRequest<ClassACommodity>();
            cQuery.Add(o => o.IsShow == true);
            cQuery.AddOrder(nameof(ClassACommodity.DisplayIndex), true);
            ViewBag.ACommodityRsp = service.GetList(cQuery);
            ViewBag.classACommodity = classACommodity;
            ViewBag.goodsName = goodsName;
            ViewBag.PageHtml = PageHepler.RenderBootstrapPagingHtml(
                 (index) => Knight.Web.Infrastructure.UrlHelper.UpdateOrAddParam(WorkContext.Url, "page", index.ToString()),
                 page,
                 rsp.TotalPages);
            ViewBag.Setting = new DbSettingApi().GetSertting<SysSetting>();
            var logInfo = new LogInfo
            {
                ModuleName = "前台访问",
                TypeName = DbLogType.Visit.ToString(),
                Result = true,
                Description = "成功",
                AddTime = DateTime.Now,
                IPAddress = Knight.Web.Infrastructure.UrlHelper.Ip
            };
            service.Add<LogInfo>(logInfo);
            return View();
        }
    }
}